package com.zomato.photofilters.imageprocessors;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

/**
 * @author Varun on 29/06/15.
 */
public final class ImageProcessor {

    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.DEBUG, 0, "ImageProcessor");

    private ImageProcessor() {
    }

    public static PixelMap applyCurves(int[] rgb, int[] red, int[] green, int[] blue,
                                       PixelMap inputImage) {
        PixelMap outputImage = inputImage;
        // create output pixelMapElememt
        int width = inputImage.getImageInfo().size.width;
        int height = inputImage.getImageInfo().size.height;

        int[] pixels = new int[width * height];
        outputImage.readPixels(pixels, 0, width, new Rect(0, 0, width, height));
        if (rgb != null) {
            pixels = NativeImageProcessor.applyRGBCurve(pixels, rgb, width, height);
        }

        if (!(red == null && green == null && blue == null)) {
            pixels = NativeImageProcessor.applyChannelCurves(pixels, red, green, blue, width, height);
        }

        try {
            PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
            initializationOptions.size = new Size(width, height);
            initializationOptions.editable = true;
            outputImage = PixelMap.create(pixels, initializationOptions);
        } catch (IllegalStateException ise) {
            throw ise;
        }
        return outputImage;
    }

    public static PixelMap doBrightness(int value, PixelMap inputImage) {
        int width = inputImage.getImageInfo().size.width;
        int height = inputImage.getImageInfo().size.height;
        int[] pixels = new int[width * height];

        inputImage.readPixels(pixels, 0, width, new Rect(0, 0, width, height));
        NativeImageProcessor.doBrightness(pixels, value, width, height);
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.size = new Size(width, height);
        initializationOptions.editable = true;
        inputImage = PixelMap.create(pixels, initializationOptions);
        return inputImage;
    }

    public static PixelMap doContrast(float value, PixelMap inputImage) {
        int width = inputImage.getImageInfo().size.width;
        int height = inputImage.getImageInfo().size.height;
        int[] pixels = new int[width * height];

        inputImage.readPixels(pixels, 0, width, new Rect(0, 0, width, height));
        NativeImageProcessor.doContrast(pixels, value, width, height);
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.size = new Size(width, height);
        initializationOptions.editable = true;
        inputImage = PixelMap.create(pixels, 0, width, initializationOptions);
        return inputImage;
    }

    public static PixelMap doColorOverlay(int depth, float red, float green, float blue,
                                          PixelMap inputImage) {
        int width = inputImage.getImageInfo().size.width;
        int height = inputImage.getImageInfo().size.height;
        int[] pixels = new int[width * height];

        inputImage.readPixels(pixels, 0, width, new Rect(0, 0, width, height));
        NativeImageProcessor.doColorOverlay(pixels, depth, red, green, blue, width, height);
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.size = new Size(width, height);
        initializationOptions.editable = true;
        inputImage = PixelMap.create(pixels, 0, width, initializationOptions);
        return inputImage;
    }

    public static PixelMap doSaturation(PixelMap inputImage, float level) {
        int width = inputImage.getImageInfo().size.width;
        int height = inputImage.getImageInfo().size.height;
        int[] pixels = new int[width * height];
        inputImage.readPixels(pixels, 0, width, new Rect(0, 0, width, height));
        NativeImageProcessor.doSaturation(pixels, level, width, height);
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.size = new Size(width, height);
        initializationOptions.editable = true;
        inputImage = PixelMap.create(pixels, 0, width, initializationOptions);
        return inputImage;
    }

}
